/**
 * 
 */
package org.vsg.stock.basic.service.volfigure.overdouble;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.vsg.stock.basic.domain.VolFigureDetails;
import org.vsg.stock.basic.domain.VolFigureKlineDaily;
import org.vsg.stock.basic.domain.VolFigureKlineDailyPK;
import org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor;
import org.vsg.stock.basic.service.volfigure.CommonParameter;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.indicator.kline.ZigIndicatorResultItem;

/**
 * 倍量柱模式"overdouble-1c-1a" 
 * @author Bill Vison
 *
 */
public class OverDouble1c1aProcessor implements VolFigureKlineDailyProcessor {
	
	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	private final double doubleValue = 2;
	
	private Set<String> dailyKeys = new HashSet<String>();
	

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#processDailyData(java.util.List)
	 */
	@Override
	public List<VolFigureKlineDaily> processDailyData(
			List<StockDailyKLine> stkDailyKLineList) {
		
		double lessValue = doubleValue - CommonParameter.DOUBLE_MERGE_BORDER_RATE;
		
		StockDailyKLine dailyKLine = null;
		StockDailyKLine preDailyKLine = null;
		
		List<VolFigureKlineDaily> klineDailyList = new Vector<VolFigureKlineDaily>();
		
		String indexLine = null;
		for (int i = stkDailyKLineList.size()-1 ; i > 0 ; i--) {
			dailyKLine = stkDailyKLineList.get(i);
			preDailyKLine = stkDailyKLineList.get(i-1);
			
			double firVolLevel = preDailyKLine.getVol() * lessValue;
			double secVolLevel = preDailyKLine.getVol() * doubleValue;
			
			if ( dailyKLine.getClose() > preDailyKLine.getClose() 
				&&	dailyKLine.getVol() < secVolLevel && dailyKLine.getVol() >= firVolLevel
				) {
				
				// --- check the value had existed ---
				indexLine = dailyKLine.getId().getStockCode()+","+detail.getCode()+","+df.format( dailyKLine.getDate() );
				
				// --- key format example "SH00001,XXXX,2010-01-10"				
				if ( !dailyKeys.contains( indexLine )) {
					
					VolFigureKlineDailyPK idPk = new VolFigureKlineDailyPK();
					idPk.setCodeVolFigureDetails( this.detail.getCode());
					idPk.setStockCode( dailyKLine.getId().getStockCode() );
					idPk.setStockStartDate( dailyKLine.getId().getDate() );
					// --- gen new data object ---
					VolFigureKlineDaily dailyInst = new VolFigureKlineDaily();
					dailyInst.setId( idPk );
					dailyInst.setStockDateEnd( dailyKLine.getId().getDate()  );
					klineDailyList.add( dailyInst );
					
				}
				
			}
			

		}


		return klineDailyList;
	}

	
	
	
	@Override
	public void setExistedVolFigureKineDaily(
			List<VolFigureKlineDaily> volFigureKlineDailyList) {
		// --- daily handle ---
		
		// --- get the daily ---
		VolFigureKlineDailyPK id = null;
		for (VolFigureKlineDaily daily : volFigureKlineDailyList) {
			id = daily.getId();
			String keyLine = id.getStockCode()+ "," + id.getCodeVolFigureDetails()+"," + df.format( id.getStockStartDate() );
			dailyKeys.add( keyLine );
		}
		
	}




	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCrestsInCycle(java.util.List)
	 */
	@Override
	public void setCrestsInCycle(List<ZigIndicatorResultItem> crests) {


	}

	
	private VolFigureDetails detail;
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCurrentVolFigureDetails(org.vsg.stock.basic.domain.VolFigureDetails)
	 */
	@Override
	public void setCurrentVolFigureDetails(VolFigureDetails detail) {
		this.detail = detail;
	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setTroughsInCycle(java.util.List)
	 */
	@Override
	public void setTroughsInCycle(List<ZigIndicatorResultItem> troughs) {
		// TODO Auto-generated method stub

	}

}
